package views;

import presenters.CreateAccountPresenter;
import com.example.wheresmystuff.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Activity for CreateAccount
 * 
 * @author Kelsey
 */
public class CreateAccountActivity extends Activity implements IFieldView {

	/** Presenter associated with activity */
	private CreateAccountPresenter myPresenter;

	/**
	 * An overwritten onCreate method
	 * 
	 * @bundle - the bundle
	 * @param savedInstanceState
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_account);
		myPresenter = new CreateAccountPresenter(this);
	}

	/**
	 * when the onCreateOptions button is pressed, this code gets called
	 * 
	 * @param menu
	 *            - the menu that will be clicked
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.create_account, menu);
		return true;
	}

	/**
	 * When the user presses onAcceptClick, this code gets called
	 * 
	 * @param source
	 *            - the source
	 */
	public void onAcceptClick(View source) {
		String name = getNameField().getText().toString();
		String phone = getPhoneField().getText().toString();
		String email = getEmailField().getText().toString();
		String pass = getPwField().getText().toString();
		String accName = getAccNameField().getText().toString();
		myPresenter.onAcceptClick(name, email, phone, accName, pass, source);
	}

	/**
	 * Returns the edited text of the getNameField
	 * 
	 * @return editText - the edit text
	 */
	private EditText getNameField() {
		return (EditText) findViewById(R.id.editName);
	}

	/**
	 * Returns the edited text of the email field
	 * 
	 * @return edittext- the edittext
	 */
	private EditText getEmailField() {
		return (EditText) findViewById(R.id.editEmail);
	}

	/**
	 * Returns the edited text of the phone field
	 * 
	 * @return - the edittext
	 */
	private EditText getPhoneField() {
		return (EditText) findViewById(R.id.editPhone);
	}

	/**
	 * Returns the edited text of the password field
	 * 
	 * @return - the edittext
	 */
	private EditText getPwField() {
		return (EditText) findViewById(R.id.editPw);
	}

	/**
	 * returns the edited text of the account field
	 * 
	 * @return - the edittext
	 */
	private EditText getAccNameField() {
		return (EditText) findViewById(R.id.editAccName);
	}

	/**
	 * when the cancel button is clicked, this code gets called
	 * 
	 * @param source
	 *            - the source
	 */
	public void onCancelClick(View source) {
		myPresenter.onCancelCreateAccountClick(source);
	}

	/**
	 * Shows failure toast
	 * 
	 * @param toast
	 *            , the toast message to show
	 */
	@Override
	public void showToast(String toast) {
		if (toast.equals("success")) {
			Toast.makeText(this, "Account Creation Successful.",
					Toast.LENGTH_SHORT).show();
		} else if (toast.equals("failure")) {
			Toast.makeText(this,
					"Account Creation Failed. Make sure everything is valid.",
					Toast.LENGTH_LONG).show();
		}
	}

	/**
	 * does nothing due to a comment
	 */
	@Override
	public void showProgressBar() {
		// findViewById(R.id.progressBar).setVisibility(0);
	}

	/**
	 * Makes the keyboard prettier
	 * 
	 * @param MotionEvent
	 *            ev - the motion oevent
	 * @return true if there is an on touch event, false otherwise
	 */
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		if (!isPointInsideView(ev.getRawX(), ev.getRawY(), getCurrentFocus())) {
			myPresenter.hideSoftKeyboard();
			return true;
		}
		return false;
	}

	/**
	 * Checks if the point is inside the view
	 * 
	 * @param x
	 *            - x position
	 * @param y
	 *            - y position
	 * @param view
	 *            - the view
	 * @return - true if the value is inside the view, false otherwise
	 */
	private boolean isPointInsideView(float x, float y, View view) {
		int location[] = new int[2];
		view.getLocationOnScreen(location);
		int viewX = location[0];
		int viewY = location[1];

		if ((x > viewX && x < (viewX + view.getWidth()))
				&& (y > viewY && y < (viewY + view.getHeight()))) {
			return true;
		} else
			return false;
	}

	/**
	 * Makes the keyboard go away
	 * 
	 * @view v - the view
	 */
	@Override
	public void hideSoftKeyboard(View v) {
		if (getCurrentFocus() != null && getCurrentFocus() instanceof EditText) {
			InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
		}
	}

	/**
	 * moves to the next intent
	 * 
	 * @param source
	 *            - the source
	 */
	@Override
	public void advance(View source) {
		Intent intent = new Intent(this, LoginActivity.class);
		finish();
		startActivity(intent);
	}
}